package o;

import android.graphics.BitmapFactory;
import android.text.SpannableStringBuilder;
import android.util.Base64;
import android.util.Pair;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.text.ttml.TtmlStyle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.TreeSet;

final class ake {
    private final HashMap<String, Integer> ʻ;
    public final String ʼ;
    @Nullable
    public final TtmlStyle ʽ;
    public final long ˊ;
    private final HashMap<String, Integer> ˊॱ;
    public final boolean ˋ;
    private List<ake> ˋॱ;
    @Nullable
    public final String ˎ;
    @Nullable
    public final String ˏ;
    public final long ॱ;
    @Nullable
    public final String ॱॱ;
    @Nullable
    private final String[] ᐝ;

    public static ake ॱ(String str) {
        return new ake(null, akf.ॱ(str), -9223372036854775807L, -9223372036854775807L, null, null, "", null);
    }

    public static ake ˊ(@Nullable String str, long j, long j2, @Nullable TtmlStyle ttmlStyle, @Nullable String[] strArr, String str2, @Nullable String str3) {
        return new ake(str, null, j, j2, ttmlStyle, strArr, str2, str3);
    }

    private ake(@Nullable String str, @Nullable String str2, long j, long j2, @Nullable TtmlStyle ttmlStyle, @Nullable String[] strArr, String str3, @Nullable String str4) {
        this.ˏ = str;
        this.ˎ = str2;
        this.ॱॱ = str4;
        this.ʽ = ttmlStyle;
        this.ᐝ = strArr;
        this.ˋ = str2 != null;
        this.ˊ = j;
        this.ॱ = j2;
        this.ʼ = (String) amm.ॱ(str3);
        this.ʻ = new HashMap();
        this.ˊॱ = new HashMap();
    }

    public boolean ॱ(long j) {
        return (this.ˊ == -9223372036854775807L && this.ॱ == -9223372036854775807L) || ((this.ˊ <= j && this.ॱ == -9223372036854775807L) || ((this.ˊ == -9223372036854775807L && j < this.ॱ) || (this.ˊ <= j && j < this.ॱ)));
    }

    public void ˎ(ake o_ake) {
        if (this.ˋॱ == null) {
            this.ˋॱ = new ArrayList();
        }
        this.ˋॱ.add(o_ake);
    }

    public ake ˊ(int i) {
        if (this.ˋॱ != null) {
            return (ake) this.ˋॱ.get(i);
        }
        throw new IndexOutOfBoundsException();
    }

    public int ˋ() {
        return this.ˋॱ == null ? 0 : this.ˋॱ.size();
    }

    public long[] ˎ() {
        TreeSet treeSet = new TreeSet();
        ˎ(treeSet, false);
        long[] jArr = new long[treeSet.size()];
        Iterator it = treeSet.iterator();
        int i = 0;
        while (it.hasNext()) {
            int i2 = i + 1;
            jArr[i] = ((Long) it.next()).longValue();
            i = i2;
        }
        return jArr;
    }

    private void ˎ(TreeSet<Long> treeSet, boolean z) {
        boolean equals = "p".equals(this.ˏ);
        boolean equals2 = "div".equals(this.ˏ);
        if (z || equals || (equals2 && this.ॱॱ != null)) {
            if (this.ˊ != -9223372036854775807L) {
                treeSet.add(Long.valueOf(this.ˊ));
            }
            if (this.ॱ != -9223372036854775807L) {
                treeSet.add(Long.valueOf(this.ॱ));
            }
        }
        if (this.ˋॱ != null) {
            for (int i = 0; i < this.ˋॱ.size(); i++) {
                boolean z2;
                ake o_ake = (ake) this.ˋॱ.get(i);
                if (z || equals) {
                    z2 = true;
                } else {
                    z2 = false;
                }
                o_ake.ˎ((TreeSet) treeSet, z2);
            }
        }
    }

    public List<Cue> ˏ(long j, Map<String, TtmlStyle> map, Map<String, akd> map2, Map<String, String> map3) {
        List<Pair> arrayList = new ArrayList();
        ˏ(j, this.ʼ, arrayList);
        Map treeMap = new TreeMap();
        ˏ(j, false, this.ʼ, treeMap);
        ˊ(j, map, treeMap);
        List arrayList2 = new ArrayList();
        for (Pair pair : arrayList) {
            String str = (String) map3.get(pair.second);
            if (str != null) {
                byte[] decode = Base64.decode(str, 0);
                akd o_akd = (akd) map2.get(pair.first);
                List list = arrayList2;
                list.add(new Cue(BitmapFactory.decodeByteArray(decode, 0, decode.length), o_akd.ˊ, 1, o_akd.ˎ, o_akd.ˋ, o_akd.ʻ, Float.MIN_VALUE));
            }
        }
        for (Entry entry : treeMap.entrySet()) {
            akd o_akd2 = (akd) map2.get(entry.getKey());
            arrayList2.add(new Cue(ॱ((SpannableStringBuilder) entry.getValue()), null, o_akd2.ˎ, o_akd2.ˏ, o_akd2.ˋ, o_akd2.ˊ, Integer.MIN_VALUE, o_akd2.ʻ, o_akd2.ʽ, o_akd2.ᐝ));
        }
        return arrayList2;
    }

    private void ˏ(long j, String str, List<Pair<String, String>> list) {
        if (!"".equals(this.ʼ)) {
            str = this.ʼ;
        }
        if (ॱ(j) && "div".equals(this.ˏ) && this.ॱॱ != null) {
            list.add(new Pair(str, this.ॱॱ));
            return;
        }
        for (int i = 0; i < ˋ(); i++) {
            ˊ(i).ˏ(j, str, list);
        }
    }

    private void ˏ(long j, boolean z, String str, Map<String, SpannableStringBuilder> map) {
        this.ʻ.clear();
        this.ˊॱ.clear();
        if (!"metadata".equals(this.ˏ)) {
            String str2 = "".equals(this.ʼ) ? str : this.ʼ;
            if (this.ˋ && z) {
                ˎ(str2, (Map) map).append(this.ˎ);
            } else if ("br".equals(this.ˏ) && z) {
                ˎ(str2, (Map) map).append('\n');
            } else if (ॱ(j)) {
                for (Entry entry : map.entrySet()) {
                    this.ʻ.put(entry.getKey(), Integer.valueOf(((SpannableStringBuilder) entry.getValue()).length()));
                }
                boolean equals = "p".equals(this.ˏ);
                for (int i = 0; i < ˋ(); i++) {
                    boolean z2;
                    ake ˊ = ˊ(i);
                    if (z || equals) {
                        z2 = true;
                    } else {
                        z2 = false;
                    }
                    ˊ.ˏ(j, z2, str2, (Map) map);
                }
                if (equals) {
                    akf.ˎ(ˎ(str2, (Map) map));
                }
                for (Entry entry2 : map.entrySet()) {
                    this.ˊॱ.put(entry2.getKey(), Integer.valueOf(((SpannableStringBuilder) entry2.getValue()).length()));
                }
            }
        }
    }

    private static SpannableStringBuilder ˎ(String str, Map<String, SpannableStringBuilder> map) {
        if (!map.containsKey(str)) {
            map.put(str, new SpannableStringBuilder());
        }
        return (SpannableStringBuilder) map.get(str);
    }

    private void ˊ(long j, Map<String, TtmlStyle> map, Map<String, SpannableStringBuilder> map2) {
        int i = 0;
        if (ॱ(j)) {
            for (Entry entry : this.ˊॱ.entrySet()) {
                int intValue;
                String str = (String) entry.getKey();
                if (this.ʻ.containsKey(str)) {
                    intValue = ((Integer) this.ʻ.get(str)).intValue();
                } else {
                    intValue = 0;
                }
                int intValue2 = ((Integer) entry.getValue()).intValue();
                if (intValue != intValue2) {
                    ॱ(map, (SpannableStringBuilder) map2.get(str), intValue, intValue2);
                }
            }
            while (i < ˋ()) {
                ˊ(i).ˊ(j, map, map2);
                i++;
            }
        }
    }

    private void ॱ(Map<String, TtmlStyle> map, SpannableStringBuilder spannableStringBuilder, int i, int i2) {
        TtmlStyle ॱ = akf.ॱ(this.ʽ, this.ᐝ, map);
        if (ॱ != null) {
            akf.ˊ(spannableStringBuilder, i, i2, ॱ);
        }
    }

    private SpannableStringBuilder ॱ(SpannableStringBuilder spannableStringBuilder) {
        int i;
        int length = spannableStringBuilder.length();
        int i2 = 0;
        while (i2 < length) {
            if (spannableStringBuilder.charAt(i2) == ' ') {
                i = i2 + 1;
                while (i < spannableStringBuilder.length() && spannableStringBuilder.charAt(i) == ' ') {
                    i++;
                }
                i -= i2 + 1;
                if (i > 0) {
                    spannableStringBuilder.delete(i2, i2 + i);
                    i = length - i;
                    i2++;
                    length = i;
                }
            }
            i = length;
            i2++;
            length = i;
        }
        if (length > 0 && spannableStringBuilder.charAt(0) == ' ') {
            spannableStringBuilder.delete(0, 1);
            length--;
        }
        i = length;
        length = 0;
        while (length < i - 1) {
            if (spannableStringBuilder.charAt(length) == '\n' && spannableStringBuilder.charAt(length + 1) == ' ') {
                spannableStringBuilder.delete(length + 1, length + 2);
                i--;
            }
            length++;
        }
        if (i > 0 && spannableStringBuilder.charAt(i - 1) == ' ') {
            spannableStringBuilder.delete(i - 1, i);
            i--;
        }
        length = 0;
        while (length < i - 1) {
            if (spannableStringBuilder.charAt(length) == ' ' && spannableStringBuilder.charAt(length + 1) == '\n') {
                spannableStringBuilder.delete(length, length + 1);
                i--;
            }
            length++;
        }
        if (i > 0 && spannableStringBuilder.charAt(i - 1) == '\n') {
            spannableStringBuilder.delete(i - 1, i);
        }
        return spannableStringBuilder;
    }
}
